import { Suspense } from 'react';
import Link from 'next/link';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { Plus, Building2, TrendingUp } from 'lucide-react';
import { formatMoney } from '@/lib/utils';
import { prisma } from '@/lib/prisma';

async function GlobalStats() {
  const totalExpenses = await prisma.expense.aggregate({
    _sum: { amount: true }
  });
  const totalContract = await prisma.site.aggregate({
    _sum: { contractAmount: true }
  });
  const activeSites = await prisma.site.count({
    where: { status: 'active' }
  });

  const totalExpensesAmount = totalExpenses._sum.amount || 0;
  const totalContractAmount = totalContract._sum.contractAmount || 0;
  const profitMargin = totalContractAmount > 0
    ? ((totalContractAmount - totalExpensesAmount) / totalContractAmount * 100).toFixed(2)
    : "0.00";

  return (
    <div className="grid gap-4 md:grid-cols-3">
      <Card>
        <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
          <CardTitle className="text-sm font-medium">总合同金额</CardTitle>
          <Building2 className="h-4 w-4 text-muted-foreground" />
        </CardHeader>
        <CardContent>
          <div className="text-2xl font-bold">{formatMoney(totalContractAmount)}</div>
          <p className="text-xs text-muted-foreground">
            {activeSites} 个进行中的工地
          </p>
        </CardContent>
      </Card>
      <Card>
        <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
          <CardTitle className="text-sm font-medium">总支出</CardTitle>
          <TrendingUp className="h-4 w-4 text-muted-foreground" />
        </CardHeader>
        <CardContent>
          <div className="text-2xl font-bold">{formatMoney(totalExpensesAmount)}</div>
          <p className="text-xs text-muted-foreground">
            剩余预算: {formatMoney(totalContractAmount - totalExpensesAmount)}
          </p>
        </CardContent>
      </Card>
      <Card>
        <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
          <CardTitle className="text-sm font-medium">平均利润率</CardTitle>
          <TrendingUp className="h-4 w-4 text-muted-foreground" />
        </CardHeader>
        <CardContent>
          <div className="text-2xl font-bold">{profitMargin}%</div>
          <p className="text-xs text-muted-foreground">
            基于所有工地数据
          </p>
        </CardContent>
      </Card>
    </div>
  );
}

export default function Home() {
  return (
    <main className="container mx-auto p-4 space-y-4">
      <div className="flex justify-between items-center">
        <h1 className="text-3xl font-bold tracking-tight">大地永兴工地财务管理</h1>
        <Button asChild>
          <Link href="/sites/new">
            <Plus className="mr-2 h-4 w-4" />
            新建工地
          </Link>
        </Button>
      </div>

      <Suspense fallback={<div>加载中...</div>}>
        <GlobalStats />
      </Suspense>

      <div className="grid gap-4 md:grid-cols-2 lg:grid-cols-3">
        <Card className="col-span-full md:col-span-2 lg:col-span-3">
          <CardHeader>
            <CardTitle>快速操作</CardTitle>
          </CardHeader>
          <CardContent className="grid gap-4 md:grid-cols-2 lg:grid-cols-4">
            <Button asChild variant="outline" className="h-24 flex-col">
              <Link href="/sites">
                <Building2 className="mb-2 h-6 w-6" />
                工地管理
              </Link>
            </Button>
            <Button asChild variant="outline" className="h-24 flex-col">
              <Link href="/expenses/new">
                <Plus className="mb-2 h-6 w-6" />
                记录支出
              </Link>
            </Button>
            <Button asChild variant="outline" className="h-24 flex-col">
              <Link href="/expenses">
                <TrendingUp className="mb-2 h-6 w-6" />
                支出记录
              </Link>
            </Button>
            <Button asChild variant="outline" className="h-24 flex-col">
              <Link href="/reports">
                <TrendingUp className="mb-2 h-6 w-6" />
                财务报表
              </Link>
            </Button>
          </CardContent>
        </Card>
      </div>
    </main>
  );
}
